package com.wang.servlet;

import com.wang.utils.ConstantsUtil;
import com.wang.utils.FileManager;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.security.spec.ECParameterSpec;

/**
 * @author wr
 * @date 2021/10/29 11:32
 */
@WebServlet(ConstantsUtil.Login.LOGIN)
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        if (user == null || "".equals(user)) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        } else {
            String s = getServletContext().getRealPath(ConstantsUtil.Storage.STORAGE_ROOT) + user;
            System.out.println("s = " + s);
            File file = new File(getServletContext().getRealPath(ConstantsUtil.Storage.STORAGE_ROOT) + user);
            if (file.exists()) {
                req.setAttribute(ConstantsUtil.Login.USER_KEY, user);
                req.getSession().setAttribute(ConstantsUtil.Login.USER_KEY, user);
            } else {
                System.out.println("新建用户文件夹");
                if (!file.mkdirs()) {
                    resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    resp.getWriter().println("创建用户文件夹错误");
                } else {
                    req.setAttribute(ConstantsUtil.Login.USER_KEY, user);
                    req.getSession().setAttribute(ConstantsUtil.Login.USER_KEY, user);
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}
